/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.Labels;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;


public class Tags extends AbstractComponent {
	

	public static final String CSSIDFIELD = "divtags";
	public static final String CSSIDCONTENT = "divtagscontent";
	public static final String NAMEFORMELEMENT = "tagsEntry";

	public static String getEmptyFieldContainer() {

		String ret = AbstractComponent.getEmptyFieldContainer( CSSIDFIELD );
		
		return ret;
	}
	
	public static String getLinkToShowSecondaryFieldInEditMode( boolean shownavigationInEditField ) {
				
		return getLinkToShowSecondaryFieldInEditMode(CSSIDFIELD, shownavigationInEditField, Labels.TAGS.getNamePlural());
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode(  URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( model.Labels.TAGS, false, true );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getContentInEditMode(uri, entry, exceptions, showNavigation);

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	

	public static String getContentInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
	
		String ret = "";
		
		ret += getTitleContainer( model.Labels.TAGS, false, true );
		ret += getFormContainer( getFormInEditMode( uri, entry, exceptions, showNavigation ), CSSIDCONTENT );

		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";	

		java.util.ArrayList<String> list = Mapper.selectTags( uri );
		String content = getTagHTMLList(list);		

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation, 
				showNavigationInEditMode );
		
		return ret;
	}
	
	private static String getTagHTMLList( java.util.ArrayList<String> entry ) {
		
		String ret = "";
		
		if( entry==null ) {
			ret += NOTHINGSELECTED;
		}
		else {
			
			for (int i=0; i<entry.size(); i++) {
				ret += "<a href=\""+ HelperFunctions.GetContextRoot() + "/Tag/" + entry.get(i) +"\">" + StringEscapeUtils.escapeHtml( entry.get(i) ) + "</a>" + HelperFunctions.TAGSSPLITTER;
			}
		}
		
		return ret.trim();	
	}
	

	
	public static String getFormInEditMode( URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null ) {

			ArrayList<String> tmp = Mapper.selectTags( uri );			
			if( tmp!=null ) {		
				actualVersionContent = getTagHTMLList( tmp );
			}
		}

		if(entry==null)
			entry="";
		
		if( !entry.equals(""))
			entry += HelperFunctions.TAGSSPLITTER;

		String content = "";
				
		content +=
			"<input autocomplete=\"off\" style=\"width: 100%\" tabindex=\"\" id=\"" + NAMEFORMELEMENT + "\" name=\"" + NAMEFORMELEMENT + "\" value=\"" + entry + "\" type=\"text\" onkeyup=\"validateValue(this,document.getElementById('" + NAMEFORMELEMENT + "_errors'))\" />";

		content +=
			"<div class=\"inputhint\">Separate your inputs with a blank symbol (e.g.: 'tag1 tag2'); write 'newYork' and not 'new York'.</div>";


		ret = renderEditForm(
				Labels.TAGS,  
				content,
				exceptions, 
				actualVersionContent, 
				actualTimestamp,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );


		return ret;
	}	

}
